package com.glodon.paas.document.dao;

import static org.junit.Assert.assertEquals;

import java.util.Date;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.glodon.paas.document.constant.DocumentConstant;
import com.glodon.paas.document.dao.domain.ShareEntity;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = { "classpath:test-document-dao.xml", "classpath:paas-document-dao-mysql.xml" })
@Ignore
public class ShareDaoMySqlIT {

    @Autowired
    ShareDao shareDao;

    @Test
    public void testCrud() {
        // insert, delete
        clean();
        init();

        // insert Duplication
        try {
            insert("default0", "f0", "u0", "t0");
            // fail("should throw insert Duplication Exception");
        } catch (Exception e) {

        }

        // get
        assertEquals("default0", shareDao.get("default0").getId());
        assertEquals(null, shareDao.get("defaultxx"));

        // getShareForMe with fileId
        assertEquals("default0", shareDao.getShareForMe("t0", "f0").get(0).getId());
        assertEquals(null, shareDao.getShareForMe("t0", "fx"));
        assertEquals(null, shareDao.getShareForMe("tx", "f0"));

        // getShareForMe without fileId
        assertEquals(0, shareDao.getShareForMe("tx", null).size());
        assertEquals(1, shareDao.getShareForMe("t0", null).size());
        assertEquals(3, shareDao.getShareForMe("t1", null).size());
        // getShareByMe with fileId
        assertEquals(1, shareDao.getShareByMe("u0", "f0", DocumentConstant.SHARE_TYPE_SHARED).size());
        assertEquals(0, shareDao.getShareByMe("u0", "fx", DocumentConstant.SHARE_TYPE_SHARED).size());
        assertEquals(0, shareDao.getShareByMe("tx", "f0", DocumentConstant.SHARE_TYPE_SHARED).size());
        assertEquals(1, shareDao.getShareByMe("u0", "f0", DocumentConstant.SHARE_TYPE_SHARED).size());
        assertEquals(2, shareDao.getShareByMe("u1", "f1", DocumentConstant.SHARE_TYPE_SHARED).size());
        assertEquals(2, shareDao.getShareByMe("u2", "f3", DocumentConstant.SHARE_TYPE_SHARED).size());

        // getShareByMe without fileId
        assertEquals(1, shareDao.getShareByMe("u0", null, DocumentConstant.SHARE_TYPE_SHARED).size());
        assertEquals(4, shareDao.getShareByMe("u1", null, DocumentConstant.SHARE_TYPE_SHARED).size());
        assertEquals(2, shareDao.getShareByMe("u2", null, DocumentConstant.SHARE_TYPE_SHARED).size());
        assertEquals(0, shareDao.getShareByMe("ux", null, DocumentConstant.SHARE_TYPE_SHARED).size());

        // countBySelective
        assertEquals(2, shareDao.getShareCount("f1", DocumentConstant.SHARE_TYPE_SHARED));

        // isSharedFile
        assertEquals(true, shareDao.isSharedFile("f0"));
        assertEquals(false, shareDao.isSharedFile("fx"));
        clean();
    }

    /*
     * init data id fileId sharedUserId toUserId default0 f0 u0 t0 default1 f1 u1 t1 default2 f1 u1 t2 default3 f2 u1 t1
     * default4 f2 u1 t2 default5 f3 u2 t1 default6 f3 u2 t2
     */
    void init() {
        insert("default0", "f0", "u0", "t0");
        insert("default1", "f1", "u1", "t1");
        insert("default2", "f1", "u1", "t2");
        insert("default3", "f2", "u1", "t1");
        insert("default4", "f2", "u1", "t2");
        insert("default5", "f3", "u2", "t1");
        insert("default6", "f3", "u2", "t2");
    }

    void clean() {
        for (int i = 0; i < 10; i++) {
            shareDao.delete("default" + i);
        }
    }

    private void insert(String id, String fileId, String sharedUserId, String toUserId) {
        ShareEntity shareEntity = new ShareEntity();
        shareEntity.setCreatedTime(new Date());
        shareEntity.setFileId(fileId);
        shareEntity.setId(id);
        shareEntity.setPrivilege(0);
        shareEntity.setSharedUserId(sharedUserId);
        shareEntity.setToUserId(toUserId);
        shareDao.insert(shareEntity);
    }
}
